package com.zhangtai.modules.service.impl.orgin;

import com.zhangtai.modules.dao.EmployeeInfoDao;
import com.zhangtai.modules.dao.OrgGroupDao;
import com.zhangtai.modules.dao.TbOrgEhrDao;
import com.zhangtai.modules.entity.orgin.TbOrgEhrEntity;
import com.zhangtai.modules.entity.system.OrgGroupEntity;
import com.zhangtai.modules.service.orgin.TbOrgEhrService;
import com.zhangtai.utils.PageUtils;
import com.zhangtai.utils.Query;
import com.zhangtai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("tbOrgEhrService")
public class TbOrgEhrServiceImpl extends ServiceImpl<TbOrgEhrDao, TbOrgEhrEntity> implements TbOrgEhrService {
    @Autowired
    private OrgGroupDao orgGroupDao;
    @Autowired
    private EmployeeInfoDao employeeInfoDao;
    @Autowired
    TbOrgEhrDao ehrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbOrgEhrEntity> page = this.page(
                new Query<TbOrgEhrEntity>().getPage(params),
                new QueryWrapper<TbOrgEhrEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 根据传入的组织id删除对应的组织  1 已删除  , 0 未删除
     *
     * @param orgid
     */

    @Override
    public void removeByOrgIds(int orgid) {
        //1. 判断是否是父类的id,还是子类的id
        TbOrgEhrEntity tbOrgEhrEntity = baseMapper.selectById(orgid);
        tbOrgEhrEntity.setIsdelete(1);
        baseMapper.updateById(tbOrgEhrEntity);
        List<TbOrgEhrEntity> list = baseMapper.selectList(new QueryWrapper<TbOrgEhrEntity>().eq("parrorgid", orgid));
        if (list.size() > 0 && list != null) {
            for (TbOrgEhrEntity t : list) {
                t.setIsdelete(1);
                this.updateById(t);
                getChildren(t.getOrgid());
            }
        }
    }

    @Override
    public TbOrgEhrEntity selectById(String orgid) {
        //1. 判断是否是父类的id,还是子类的id
        TbOrgEhrEntity tbOrgEhrEntity = baseMapper.selectById(orgid);
        if (tbOrgEhrEntity != null) {
            List<TbOrgEhrEntity> list = baseMapper.selectList(new QueryWrapper<TbOrgEhrEntity>().eq("parrorgid", orgid));
             getChildrens(list, tbOrgEhrEntity);
        }
        return tbOrgEhrEntity;
    }

    public TbOrgEhrEntity getChildrens(List<TbOrgEhrEntity> list, TbOrgEhrEntity tbOrgEhrEntity) {
        if (list.size() >= 1 && list != null) {
            tbOrgEhrEntity.setChildren(list);
            for (TbOrgEhrEntity l : list) {
                List<TbOrgEhrEntity> list1 = baseMapper.selectList(new QueryWrapper<TbOrgEhrEntity>().eq("parrorgid", l.getOrgid()));
                getChildrens(list1, l);
            }
            return tbOrgEhrEntity;
        }
        return null;
    }

    //循环遍历
    public void getChildren(int orgid) {
        List<TbOrgEhrEntity> list = baseMapper.selectList(new QueryWrapper<TbOrgEhrEntity>().eq("parrorgid", orgid));
        if (list.size() > 0 && list != null) {
            for (TbOrgEhrEntity t : list) {
                t.setIsdelete(1);
                this.updateById(t);
            }
        }
    }
}